//
//  MCDeliveryInTimeItemView.swift
//  StoreManage
//
//  Created by MC on 2017/12/28.
//  Copyright © 2017年 MC. All rights reserved.
//

import UIKit

class MCDeliveryInTimeItemView: UIView {

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(addButton)
        addButton.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(0)
            make.centerY.equalTo(self)
            make.size.equalTo(CGSize.init(width: 20, height: 20))
        }
        
        self.addSubview(countTextField)
        countTextField.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(addButton.snp.left).offset(-5)
            make.centerY.equalTo(self)
            make.size.equalTo(CGSize.init(width: 30, height: 30))
        }
        
        self.addSubview(reduceButton)
        reduceButton.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(countTextField.snp.left).offset(-5)
            make.centerY.equalTo(self)
            make.size.equalTo(CGSize.init(width: 20, height: 20))
        }

        self.addSubview(titleLabel)
        titleLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(0)
            make.right.equalTo(reduceButton.snp.left).offset(-10)
            make.centerY.equalTo(self)
        }
    }
    
    @objc func reduceButtonClicked() {
        var result = Int(countTextField.text!)!
        result -= 1
        if result <= 0 {
            result = 0
        }
        countTextField.text = String(result)
    }
    
    @objc func addButtonClicked() {
    
        
        var result = Int(countTextField.text!)!
        result += 1
        countTextField.text = String(result)

    }

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(14)
        label.text = "镖旗"
        return label
    }()
    
    lazy var addButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.setImage(#imageLiteral(resourceName: "Reuse_add"), for: UIControlState.normal)
        button.addTarget(self, action: #selector(addButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()
    
    lazy var reduceButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.setImage(#imageLiteral(resourceName: "Reuse_reduce"), for: UIControlState.normal)
        button.addTarget(self, action: #selector(reduceButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()

    lazy var countTextField: UITextField = {
        let textField = UITextField()
        textField.font = MCFont(14)
        textField.textAlignment = NSTextAlignment.center
        textField.keyboardType = UIKeyboardType.numberPad
        return textField
    }()
}
